package com.atguigu.lease.admin.service.impl;

import com.atguigu.lease.admin.service.GraphInfoService;
import com.atguigu.lease.admin.service.RoomAttrValueService;
import com.atguigu.lease.admin.service.RoomFacilityService;
import com.atguigu.lease.admin.service.RoomInfoService;
import com.atguigu.lease.admin.service.RoomLabelService;
import com.atguigu.lease.admin.service.RoomLeaseTermService;
import com.atguigu.lease.admin.service.RoomPaymentTypeService;
import com.atguigu.lease.model.bean.*;
import com.atguigu.lease.model.dto.RoomItemQueryDto;
import com.atguigu.lease.model.dto.RoomSubmitDto;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.vo.RoomItemVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.admin.mapper.RoomInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author 1
* @description 针对表【room_info(房间信息表)】的数据库操作Service实现
* @createDate 2025-09-22 17:18:51
*/
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
    implements RoomInfoService {

    @Autowired
    RoomFacilityService roomFacilityService;
    private final RoomFacilityServiceImpl roomFacilityServiceImpl;

    @Autowired
    RoomFacilityService facilityService;

    @Autowired
    RoomAttrValueService roomAttrValueService;

    @Autowired
    RoomLabelService roomLabelService;

    @Autowired
    RoomLeaseTermService roomLeaseTermService;

    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    GraphInfoService graphInfoService;

    public RoomInfoServiceImpl(RoomFacilityServiceImpl roomFacilityServiceImpl) {
        this.roomFacilityServiceImpl = roomFacilityServiceImpl;
    }

    @Override
    public Page<RoomItemVo> pageItem(RoomItemQueryDto queryDto) {
        //使用queryWrapper代替mybatis中的自定义sql
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(queryDto.getProvinceId()) , "t3.province_id" ,
                queryDto.getProvinceId());
        queryWrapper.eq(StringUtils.hasText(queryDto.getCityId()) , "t3.city_id" ,
                queryDto.getCityId());
        queryWrapper.eq(StringUtils.hasText(queryDto.getDistrictId()) , "t3.district_id" ,
                queryDto.getDistrictId());
        queryWrapper.eq("t1.is_deleted" , 0 );
        queryWrapper.eq(StringUtils.hasText(queryDto.getApartmentId()) , "t1.apartment_id" ,
                queryDto.getApartmentId());

        Page<RoomItemVo> page =   baseMapper.selectPageItem(new Page<>(queryDto.getCurrent(), queryDto.getSize()),
                queryWrapper);
        return page;
    }
    @Override

    public void saveOrUpdateRoomSubmitDto(RoomSubmitDto roomSubmitDto) {
        //1.判断是新增还是更新
        boolean isUpdate = roomSubmitDto.getId() != null;
        //2.新增或更新房间信息
        saveOrUpdate(roomSubmitDto);
        Long roomId = roomSubmitDto.getId();

        //3.如果是更新：需要先删除房间所有的关联数据
         if (isUpdate){
             removeRoomDatas(roomId);
         }
        //4.无论是新增还是更新：保存本次页面提交的房间的关联数据
        //保存房间的配套关联数据
//        List<Integer> facilityInfoIds = roomSubmitDto.getFacilityInfoIds();
//        List<RoomFacility> roomFacilities = facilityInfoIds.stream()
//               .map(facilityInfoId -> {
//                    RoomFacility roomFacility = new RoomFacility();
//                    roomFacility.setRoomId(roomId);
//                    roomFacility.setFacilityId(facilityInfoId.longValue());
//                    return roomFacility;
//                }
//        ).collect(Collectors.toList());
//        roomFacilityService.saveBatch(roomFacilities);
        List<Integer> facilityInfoIds = roomSubmitDto.getFacilityInfoIds();
        List<RoomFacility> roomFacilities = facilityInfoIds.stream().map(
                new Function<Integer, RoomFacility>() {
                    @Override
                    public RoomFacility apply(Integer facilityInfoId) {
                        RoomFacility roomFacility = new RoomFacility();
                        roomFacility.setRoomId(roomId);
                        roomFacility.setFacilityId(facilityInfoId.longValue());
                        return roomFacility;
                    }
                }
        ).collect(Collectors.toList());
        roomFacilityService.saveBatch(roomFacilities);



        //保存房间的属性值关联数据
        List<Integer> attrValueIds = roomSubmitDto.getAttrValueIds();
        List<RoomAttrValue> collect = attrValueIds.stream()
                .map(attrValueId -> {
                            RoomAttrValue roomAttrValue = new RoomAttrValue();
                            roomAttrValue.setAttrValueId(Long.valueOf(attrValueId));
                            roomAttrValue.setRoomId(roomId);
                            return roomAttrValue;
                        }
                ).collect(Collectors.toList());
        roomAttrValueService.saveBatch(collect);


        //保存房间的标签关联数据

        List<Integer> labelInfoIds = roomSubmitDto.getLabelInfoIds();
        List<RoomLabel> roomLabels = labelInfoIds.stream().map(
                labelInfoId -> {
                    RoomLabel roomLabel = new RoomLabel();
                    roomLabel.setRoomId(roomId);
                    roomLabel.setLabelId(labelInfoId.longValue());
                    return roomLabel;
                }
        ).collect(Collectors.toList());
        roomLabelService.saveBatch(roomLabels);


        //保存房间的可选租约的关联数据
        //整体看就是给List集合里的每一个对象的属性赋值，然后再返回list集合
        List<Integer> leaseTermIds = roomSubmitDto.getLeaseTermIds();
        List<RoomLeaseTerm> list = leaseTermIds.stream().map(
                labelInfoId -> {
                    RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
                    roomLeaseTerm.setLeaseTermId(labelInfoId.longValue());
                    roomLeaseTerm.setRoomId(roomId);
                    return roomLeaseTerm;
                }
        ).collect(Collectors.toList());

        roomLeaseTermService.saveBatch(list);

        //保存房间的可选支付方式的关联数据
        List<Integer> paymentTypeIds = roomSubmitDto.getPaymentTypeIds();
        List<RoomPaymentType> collect1 = paymentTypeIds.stream().map(
                paymentTypeId -> {
                    RoomPaymentType roomPaymentType = new RoomPaymentType();
                    roomPaymentType.setRoomId(roomId);
                    roomPaymentType.setPaymentTypeId(paymentTypeId.longValue());
                    return roomPaymentType;
                }
        ).collect(Collectors.toList());
        roomPaymentTypeService.saveBatch(collect1);


    }

    //根据房间id删除 房间关联数据的 业务
    private void removeRoomDatas(Long roomId) {
        //根据房间id删除配套关联数据
        roomFacilityService.remove(Wrappers.lambdaQuery(RoomFacility.class)
                .eq(RoomFacility::getRoomId, roomId));
        //根据房间id删除属性值关联数据
        roomAttrValueService.remove(Wrappers.lambdaQuery(RoomAttrValue.class)
                .eq(RoomAttrValue::getRoomId, roomId));
        //根据房间id删除标签关联数据
        roomLabelService.remove(Wrappers.lambdaQuery(RoomLabel.class)
                .eq(RoomLabel::getRoomId, roomId));
        //根据房间id删除可选租约关联数据
        roomLeaseTermService.remove(Wrappers.lambdaQuery(RoomLeaseTerm.class)
                .eq(RoomLeaseTerm::getRoomId, roomId));
        //根据房间id删除可选支付方式关联数据
        roomPaymentTypeService.remove(Wrappers.lambdaQuery(RoomPaymentType.class)
                .eq(RoomPaymentType::getRoomId, roomId));
        //根据房间id+类型 删除房间的图片列表
        graphInfoService.remove(Wrappers.lambdaQuery(GraphInfo.class)
                .eq(GraphInfo::getItemId, roomId)
                .eq(GraphInfo::getItemType , ItemType.ROOM));

    }


}

